const app = getApp();

Page({
  data: {
    tagss: {},
    formData: {
      title: '',
      date: '',
      tags: [],
      duration: '1',
      conflictLevel: 3,
      responsibility: 'partner',
      status: 'resolved',
      solution: '',
      couponType: 'movie',
      couponDescription: '',
      couponExpiry: '',
      couponStatus: 'unused',
      details: ''
    },
    tagOptions: getApp().globalData.enums.tagOptions,
    conflictLabels: getApp().globalData.enums.conflictLabels,
    responsibilityOptions: getApp().globalData.enums.responsibilityOptions,
    statusOptions: getApp().globalData.enums.statusOptions,
    couponOptions: getApp().globalData.enums.couponOptions
  },
  
  onTitleChange(e) {
    this.setData({
      'formData.title': e.detail.value
    });
  },
  
  onDateChange(e) {
    this.setData({
      'formData.date': e.detail.value
    });
  },
  
  onDurationChange(e) {
    let duration = e.detail.value;
    // 确保时长在1-240小时之间
    if (duration < 0) duration = null;
    if (duration > 240) duration = 240;
    this.setData({
      'formData.duration': duration
    });
  },
  
  toggleTag(e) {
    const tag = e.currentTarget.dataset.tag;
    
    if (!tag) return;

    this.setData({
      'formData.tags': this.data.formData.tags.includes(tag) 
        ? this.data.formData.tags.filter(t => t !== tag)
        : [...this.data.formData.tags, tag],
        'tagss': {
          ...this.data.tagss,
          [tag]: !this.data.formData.tags.includes(tag) ? "active-tag" : "tag"
        }
    });

  },
  
  setConflictLevel(e) {
    const level = parseInt(e.currentTarget.dataset.level);
    this.setData({
      'formData.conflictLevel': level
    });
  },
  
  setResponsibility(e) {
    const value = e.currentTarget.dataset.value;
    this.setData({
      'formData.responsibility': value
    });
  },
  
  setStatus(e) {
    const value = e.currentTarget.dataset.value;
    this.setData({
      'formData.status': value
    });
  },
  
  onSolutionChange(e) {
    this.setData({
      'formData.solution': e.detail.value
    });
  },
  
  setCouponType(e) {
    const value = e.currentTarget.dataset.value;
    this.setData({
      'formData.couponType': value
    });
  },
  
  onCouponDescriptionChange(e) {
    this.setData({
      'formData.couponDescription': e.detail.value
    });
  },
  
  onCouponExpiryChange(e) {
    this.setData({
      'formData.couponExpiry': e.detail.value
    });
  },
  
  onDetailsChange(e) {
    this.setData({
      'formData.details': e.detail.value
    });
  },
  
  goBack() {
    wx.navigateBack();
  },
  
  saveRecord() {
    const formData = this.data.formData;
    
    // 验证必填字段
    if (!formData.title.trim()) {
      wx.showToast({
        title: '请填写吵吵主题',
        icon: 'none'
      });
      return;
    }
    
    if (!formData.date) {
      wx.showToast({
        title: '请选择发生日期',
        icon: 'none'
      });
      return;
    }
    
    // 保存记录
    const recordWithTags = {...formData, category: formData.tags.join(',')};
    const newRecord = app.addRecord(recordWithTags);
    
    wx.showToast({
      title: '保存成功',
      icon: 'success',
      duration: 1500,
      complete: () => {
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      }
    });
  }
});